using System;
using System.Configuration;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using AWS.SLAM;
using AWS.SLAM.Activation;
using AWS.SLAM.Configuration;
using Microsoft.SharePoint;

namespace AWS.SLAM
{
    /// <summary>
    /// The SLAM FeatureReceiver performs tasks for activating and deactivating SLAM.  By default,
    /// on activation SLAM attaches its event handlers to all configured Lists and Content Types and
    /// deactivation detaches those event handlers.
    /// </summary>

    [Guid("56b01993-0ef6-436c-baa9-bd347f2dc174")]
    public class FeatureReceiver : SPFeatureReceiver
    {
        public override void FeatureInstalled(SPFeatureReceiverProperties properties) { }
        public override void FeatureUninstalling(SPFeatureReceiverProperties properties) { }

        /// <summary>
        /// FeaturedActivated is called when the SLAM feature is activated and in turn calls
        /// SLAM's IActivator.Activate.  By default Activate attaches event handlers to all configured
        /// Lists and Content Types.
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            //ComponentService.ClearInstances();
            //IActivator activator = ComponentService.GetInstance<IActivator>();
            //activator.Activate();

            //If activation fails we want it to throw all exceptions so that 
            //the user activating this feature will see them directly
        }

        /// <summary>
        /// FeaturedDeactivated is called when the SLAM feature is deactivated and in turn calls
        /// SLAM's IActivator.Deactivate.  By default Deactivate detaches event handlers from all configured
        /// Lists and Content Types.
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            if (ConfigurationManager.AppSettings != null)
            {
                bool deactivate = true;
                try
                {
                    ComponentService.ClearInstances();
                }
                catch
                {
                    //if ClearInstances runs into a problem it is most likely that
                    //the Configuration cannot be found, in which case we want to
                    //allow the feature to deactivate but not run SLAM's Deactivate process.
                    deactivate = false;
                }
                if (deactivate)
                {
                    ComponentService.ClearInstances();
                    IActivator activator = ComponentService.GetInstance<IActivator>();
                    activator.Deactivate();

                    //If activation fails we want it to throw all exceptions so that 
                    //the user deactivating this feature will see them directly
                }
            }
        }
    }
}
